﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

namespace TR0217.ControlEx
{
    [ToolboxBitmap(typeof(TextBox))]
    public class TransparentTextBox:TextBox
    {
        public TransparentTextBox()
        {
        }

        protected override void OnPaintBackground(PaintEventArgs pevent)
        {
            base.OnPaintBackground(pevent);
            if (this.BackColor != Color.Transparent)
            {
                this.Parent.Invalidate(this.Region);
                Graphics g = pevent.Graphics;
                if (Parent != null)
                {
                    int index = Parent.Controls.GetChildIndex(this);

                    for (int i = Parent.Controls.Count - 1; i > index; i--)
                    {
                        Control c = Parent.Controls[i];
                        if (c.Bounds.IntersectsWith(Bounds) && c.Visible)
                        {
                            Bitmap bmp = new Bitmap(c.Width, c.Height, g);
                            c.DrawToBitmap(bmp, c.ClientRectangle);

                            g.TranslateTransform(c.Left - Left, c.Top - Top);
                            g.DrawImageUnscaled(bmp, Point.Empty);
                            g.TranslateTransform(Left - c.Left, Top - c.Top);
                            bmp.Dispose();
                        }
                    }
                }
            }
        }
            
        //protected override void WndProc(ref Message m)
        //{
        //    base.WndProc(ref m);
        //    switch (m.Msg)
        //    {
        //        case 0xF:

        //            Bitmap bmpCaptured =
        //              new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height);
        //            Bitmap bmpResult =
        //              new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height);
        //            Rectangle r =
        //              new Rectangle(0, 0, this.ClientRectangle.Width,
        //              this.ClientRectangle.Height);

        //            CaptureWindow(this, ref bmpCaptured);
        //            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
        //            this.BackColor = Color.Transparent;

        //            ImageAttributes imgAttrib = new ImageAttributes();

        //            ColorMap[] colorMap = new ColorMap[1];

        //            colorMap[0] = new ColorMap();

        //            colorMap[0].OldColor = Color.White;

        //            colorMap[0].NewColor = Color.Transparent;

        //            imgAttrib.SetRemapTable(colorMap);

        //            Graphics g = Graphics.FromImage(bmpResult);

        //            g.DrawImage(bmpCaptured, r, 0, 0, this.ClientRectangle.Width,
        //                this.ClientRectangle.Height, GraphicsUnit.Pixel, imgAttrib);

        //            g.Dispose();

        //            pictureBox.Image = (Image)bmpResult.Clone();
        //            break;


        //        case 0x114:

        //        case 0x115:

        //            this.Invalidate(); // repaint
        //            // if you use scrolling then add these two case statements

        //            break;
        //    }
        //}
        //[DllImport("user32.dll")]
        //public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);
        //private static void CaptureWindow(Control control, ref Bitmap bitmap)
        //{
        //    Graphics g = Graphics.FromImage(bitmap);
        //    int i = (int)(0x00000004L | 0x00000008L);
        //    IntPtr iPtr = new IntPtr(14);
        //    IntPtr hdc = g.GetHdc();
        //    SendMessage(control.Handle, 0xF, hdc, iPtr);
        //    g.ReleaseHdc(hdc);
        //    g.Dispose();
        //}
    }
}
